﻿using Antlr.Runtime.Tree;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KaoShifuxi02
{
    public partial class _Default : Page
    {
        private DataTable Data
        {
            get
            {
                var sql = "select * from Products";
                var dt = Dbhelper.GetDataTable(sql);
                return dt
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //var dt = new DataTable();
            //dt.Columns.Add(new DataColumn("Id"));
            //dt.Columns.Add(new DataColumn("ProductName"));
            //dt.Columns.Add(new DataColumn("ShortDesc"));
            //dt.Columns.Add(new DataColumn("Remarks"));
            //foreach (GridViewRow row in gv1.Rows)
            //{
            //   var tempRow = dt.NewRow();
            //    tempRow["Id"] = GetValue(row.RowIndex, 0).Replace("&nbsp", "");
            //    tempRow["ProductName"] = GetValue(row.RowIndex, 0).Replace("&nbsp", "");
            //    tempRow["ShortDesc"] = GetValue(row.RowIndex, 0).Replace("&nbsp", "");
            //   tempRow["Remarks"] = GetValue(row.RowIndex, 0).Replace("&nbsp", "");
            //    dt.Rows.Add(tempRow);
            // }
            //dt.Rows.Add(dt.NewRow());
            //gv1.DataSource = dt;
            //gv1.DataBind();
            //gv1.EditIndex = dt.Rows.Count - 1;
            //gv1.DataSource = dt;
            //gv1.DataBind();

            var tempDt = this.Data;
            var dt = tempDt.Copy();
            dt.Rows.Add(dt.NewRow());
            gv1.DataSource = dt;
            gv1.DataBind();
            gv1.EditIndex = dt.Rows.Count - 1;
            gv1.DataSource = dt;
            gv1.DataBind();


        }

        private void FillData()
        {
           
            gv1.DataSource = this.Data;
            gv1.DataBind();
        }
        private string GetValue(int rowIndex,int colIndex)
        {
            var c = gv1.Rows[rowIndex].Cells[colIndex];
            var value = c.Controls.Count > 0 ? ((TextBox)c.Controls[0]).Text : c.Text;
            return value;
        }

        protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv1.EditIndex = -1;
            FillData();
        }

        protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var sql = string.Format("delete from Products where id='{0}'",id);
            Dbhelper.Exc(sql);
            gv1.EditIndex = -1;
            FillData();
        }

        protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv1.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetValue(e.RowIndex, 0);
            var isOk = int.TryParse(idString, out int id);
            var ProductName = GetValue(e.RowIndex, 1);
            var ShortDesc = GetValue(e.RowIndex, 2);
            var Remarks = GetValue(e.RowIndex, 3);
            if (isOk)
            {
                var sql = string.Format("update Products set ProductName='{0}',ShortDesc='{1}',Remarks='{2}' where id='{3}'", ProductName, ShortDesc, Remarks, id);
                Dbhelper.Exc(sql);
            }
            else
            {
                var sql = string.Format("insert into Products(ProductName,ShortDesc,Remarks)values('{0}','{1}','{2}')", ProductName, ShortDesc, Remarks);
                Dbhelper.Exc(sql);
            }
            gv1.EditIndex = -1;
            FillData();
        }
    }
}